<?php
namespace App\Controller;
use Cake\Cache\Cache;

class AdminGroupsController extends AppController{

    public function initialize()
    {
        parent::initialize();
    }

    /*
     * create by phpstorm
     * author：Ming
     * date：2018.06.27
     * ps：获取管理组信息
     * */
    public function index()
    {
        $groups =  $this->AdminGroups->find()
            ->order(['id asc'])
            ->select(['id', "alias", "acos"])
            ->order(['id asc'])
            ->toArray();
        foreach ($groups as $key => $value){
            $groups[$key]["acos"] = json_decode($value["acos"],true);
        }
        $list['list'] = $groups;
        $this->G->success('Success',$list);
    }

    /*
     * create by phpstorm
     * author：Ming
     * date：2018.06.27
     * ps：管理组列表
     * */
    public function lists()
    {
        $groups =  $this->AdminGroups->find()
            ->order(['id asc'])
            ->select(['id', "alias"])
            ->order(['id asc'])
            ->toArray();
        foreach ($groups as $key => $value){
            $groups[$key]["acos"] = json_decode($value["acos"],true);
        }
        $list['list'] = $groups;
        $this->G->success('Success',$list);
    }

    /*
     * create by phpstorm
     * author：Ming
     * date：2018.06.27
     * ps：新增管理组
     * */
    public function add()
    {
        if(!$this->request->is("post")){
            $this->G->error("post_error");
            return null;
        }
        $alias = $this->request->getData("alias");
        if(empty(trim($alias))){
            $this->G->error("alias_empty");
            return null;
        }
        $groups_db = $this->AdminGroups->find()
            ->select(["id"])
            ->where(["alias" => $alias])
            ->first();
        if(!empty($groups_db)){
            $this->G->error("alias_exist");
            return null;
        }
        $acos = $this->request->getData("acos");
        $group_acos = $acos;
        $acos = json_encode($acos,true);
        $uid = $this->Jwt->get("user_id");
        $this->loadModel('Admins');
        $admin = $this->Admins->findById($uid)->select(["id","username"])->first();
        $username = $admin->username;
        $groups = $this->AdminGroups->newEntity();
        $groups->alias = $alias;
        $groups->acos = $acos;
        $groups->creattime = time();
        $groups->creatuser = $username;
        $group_save = $this->AdminGroups->save($groups);
        $gid = $group_save->id;
        Cache::write('group_'.$gid, $group_acos,'f1y');
        $this->G->success('Success');
    }

    /*
     * create by phpstorm
     * author：Ming
     * date：2018.06.27
     * ps：修改管理组
     * */
    public function update($gid = null)
    {
        if(!$this->request->is("post")){
            $this->G->error("post_error");
            return null;
        }
        if(!is_numeric($gid)){
            $this->G->error("gid_error");
            return null;
        }
        $groups = $this->AdminGroups->newEntity();
        $alias = $this->request->getData("alias");
        if(!empty(trim($alias))){
            $groups->alias = $alias;
        }
        $acos = $this->request->getData("acos");
        $group_acos = $acos;
        if(!empty($acos)){
            $acos = json_encode($acos,true);
            $groups->acos = $acos;
        }
        $groups_db = $this->AdminGroups->findById($gid)->select(["id"])->first();
        if(empty($groups_db)){
            $this->G->error("group_not_found");
            return null;
        }
        if($gid == 1){
            $this->G->error("group_not_modify");
            return null;
        }
        $groups_db = $this->AdminGroups->find()
            ->select(["id"])
            ->where(["alias" => $alias,"id !=" => $gid])
            ->first();
        if(!empty($groups_db)){
            $this->G->error("alias_exist");
            return null;
        }
        $uid = $this->Jwt->get("user_id");
        $this->loadModel('Admins');
        $admin = $this->Admins->findById($uid)->select(["id","username"])->first();
        $username = $admin->username;
        $groups->id = $gid;
        $groups->creattime = time();
        $groups->creatuser = $username;
        $group_save = $this->AdminGroups->save($groups);
        Cache::write('group_'.$gid, $group_acos,'f1y');
        $this->G->success('Success');
    }

    /*
     * create by phpstorm
     * author：Ming
     * date：2018.06.27
     * ps：删除管理组
     * */
    public function delete($gid = null)
    {
        if(!is_numeric($gid)){
            $this->G->error("gid_error");
            return null;
        }
        $groups =  $this->AdminGroups->findById($gid)->select(["id"])->first();
        if(!$groups){
            $this->G->error("group_not_found");
            return null;
        }
        if($gid == 1){
            $this->G->error("group_not_modify");
            return null;
        }
        $this->loadModel('Admins');
        $admins =  $this->Admins->find()->select(["id"])->where(["group_id" => $gid])->first();
        if($admins){
            $this->G->error("group_user_found");
            return null;
        }
        $this->AdminGroups->deleteAll(['id'=>$gid]);
        Cache::delete('group_'.$gid,'f1y');
        $this->G->success('Success');
    }

}
?>



